import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicStampedReference;

/**
 * @author brianxia
 * @version 1.0
 * @date 2022-10-22 15:35
 */

//CAS compare and swap 比较和交换
public class Demo6 {

    public static AtomicStampedReference count = new AtomicStampedReference<String>("空",1);

    //线程安全概念  案例线程不安全
    public static void main(String[] args) throws InterruptedException {

//        Thread thread1 = new Thread(() -> {
//            for (int i = 0; i < 100000; i++) {
//                count.incrementAndGet();
//            }
//        });
//
//        Thread thread2 = new Thread(() -> {
//            for (int i = 0; i < 100000; i++) {
//                count.incrementAndGet();
//            }
//        });
//
//        thread1.start();
//        thread2.start();
//
//        //等待线程结束
//        thread1.join();
//        thread2.join();
//
//        System.out.println(count.get());

        boolean b = count.compareAndSet("空", "老王", 1, 2);
        System.out.println(b);
        b = count.compareAndSet("老王", "空", 2, 3);
        System.out.println(b);
        b = count.compareAndSet("空", "小李", 1, 2);
        System.out.println(b);

    }
}
